//
//  LSCirleColorPicker.m
//  PersonalTool
//
//  Created by Samuel on 3/23/15.
//  Copyright (c) 2015 Samuel. All rights reserved.
//

#import "LSCirleColorPicker.h"
#import "LSColorGeter.h"
@implementation LSCirleColorPicker
{
    UIImageView *cirleImage;
    UIImageView *pointes;
    UIColor *getColor;
}


//- (id) initWithFrame:(CGRect)frame;
//{
//    if(self = [super init])
//    {
//        [self setBackgroundImage:@"pickerColorWheel@2x" andFrame:frame];
//        
//        
//    }
//    return self;
//}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}



- (void)setup
{
    cirleImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pickerColorWheel"]];
    [cirleImage setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:cirleImage];
    cirleImage.backgroundColor = [UIColor redColor];
    cirleImage.clipsToBounds = YES;
    [cirleImage.layer setCornerRadius:CGRectGetHeight([cirleImage bounds]) / 2];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];

    [cirleImage addGestureRecognizer:pan];
    [cirleImage setUserInteractionEnabled:YES];
    pointes = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"colorPickerKnob@2x"]];
    pointes.center = cirleImage.center;
    [cirleImage addSubview:pointes];
    
    NSLog(@"%f,%f,%f,%f",cirleImage.layer.frame.origin.x,cirleImage.layer.frame.origin.y,cirleImage.layer.frame.size.width,cirleImage.layer.frame.size.height);
}
/* 识别拖动 */
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer {
    CGPoint location = [gestureRecognizer locationInView:cirleImage];
    NSLog(@"%f,%f",location.x,location.y);
    
    float radius = cirleImage.bounds.size.width/2;
    double tapradius =  hypot(location.x-cirleImage.center.x, location.y-cirleImage.center.y);
    if (tapradius>radius) {
        return;
    }
//    if (!CGRectContainsPoint(cirleImage.layer.frame, location))
//    {
//        return;
//    }
   
    pointes.center = location;
    LSColorGeter *LCG = [[LSColorGeter alloc]init];
    getColor = [LCG getPixelColorAtLocation:location andImage:cirleImage.image];
    if ([self.colorDelegate respondsToSelector:@selector(returnTheUIColor:)] )
    {
        [self.colorDelegate returnTheUIColor:getColor] ;
    }
}

















@end
